package com.citywithincity.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.citywithincity.api.ApiFilter;



public class PathUtil {
	
	private static String webRootPath;
	private static String rootClassPath;
	
	
	/**
	 * 获取网站根目录
	 * @return
	 */
	public static String getWebRootPath(){
		if(webRootPath==null){
			try {
				String path = PathUtil.class.getResource("/").toURI().getPath();
				webRootPath = new File(path).getParentFile().getParentFile().getCanonicalPath();
				if(!webRootPath.endsWith(File.separator)){
					webRootPath = new StringBuilder(webRootPath).append( File.separator ).toString();
				}
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return webRootPath;
		
	}

	public static String getRootClassPath() {
		if (rootClassPath == null) {
			try {
				String path = PathUtil.class.getClassLoader().getResource("").toURI().getPath();
				rootClassPath = new File(path).getAbsolutePath();
			}
			catch (Exception e) {
				String path = PathUtil.class.getClassLoader().getResource("").getPath();
				rootClassPath = new File(path).getAbsolutePath();
			}
		}
		return rootClassPath;
	}
	
	
	
	public static void createDir(String path){
		
	}

	public static String getUploadPath(String path) {
		return new StringBuilder(getWebRootPath()).append(path).toString();
	}
	
	/**
	 * 真实路径
	 * @param path
	 * @return
	 */
	public static String getRealPath(String path) {
		return new StringBuilder(getWebRootPath()).append(path).toString();
	}
	
	/**
	 * 获取相对路径
	 * @param path
	 * @return
	 */
	public static String getRelativePath(String path){
		return new StringBuilder(100).append('/').append(ApiFilter.me().getContextPath())
				.append(path.substring(webRootPath.length())).toString();
	}
	
	public static InputStream getConfigInputStream(Class<?> clazz,String name) throws FileNotFoundException{
		try{
			return getInputStream(clazz,name);
		}catch(FileNotFoundException e){
			String[] arr = name.split("\\.");
			String n = arr[0];
			String ext = arr[1];
			String configName = ConfigReader.getDefault().get("configName");
			if(configName==null){
				throw e;
			}
			return getInputStream(clazz,n+ "-" +configName + "." + ext);
		}
	}
	
	public static InputStream getInputStream(Class<?> clazz,String name) throws FileNotFoundException{
		InputStream in = clazz.getClassLoader().getResourceAsStream(name);
		if(in==null){
			in = new FileInputStream(PathUtil.getConfigPath(name));
		}
		return in;
	}
	
	/**
	 * 获取webinf下的配置文件位置
	 * @param name
	 */
	public static File getConfigPath(String name) {
		File file = new File( PathUtil.getWebRootPath() + "src/main/webapp/WEB-INF" + File.separator + name);
		if(!file.exists()){
			file = new File( PathUtil.getWebRootPath() + "WEB-INF" + File.separator + name);
		}
		return file;
	}
	
	public static File getLibJar(String name) {
		File file = new File( PathUtil.getWebRootPath() + "src/main/webapp/WEB-INF/lib/" + name);
		if(!file.exists()){
			file = new File( PathUtil.getWebRootPath() + "WEB-INF/lib/"+ name);
		}
		return file;
	}

	public static File getWebInfPath(String name) {
		File file = new File( PathUtil.getWebRootPath() + "src/main/webapp/WEB-INF" + File.separator + name);
		if(!file.exists()){
			file = new File( PathUtil.getWebRootPath() + "WEB-INF" + File.separator + name);
		}
		return file;
	}

	
}
